package com.mbyte.easy.admin.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.mbyte.easy.admin.entity.Draft;
import com.mbyte.easy.admin.entity.DraftLog;
import com.mbyte.easy.admin.service.IDraftService;
import com.mbyte.easy.common.constant.DraftIsPass;
import com.mbyte.easy.common.constant.DraftLogConstant;
import com.mbyte.easy.common.constant.DraftType;
import com.mbyte.easy.common.constant.StateDic;
import com.mbyte.easy.common.controller.BaseController;
import com.mbyte.easy.common.web.AjaxResult;
import com.mbyte.easy.util.PageInfo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationEventPublisher;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import java.util.Map;

/**
 * @ClassNamePartnerCheckController
 * @Description 互查伙伴审核
 * @Author myr
 * @Date 2019/12/10 10:26
 * @Version 1.0
 **/
@Slf4j
@Controller
@RequestMapping("/draft/partner")
public class PartnerCheckController extends BaseController {

    private String prefix = "admin/draft/writer/";

    @Autowired
    private IDraftService draftService;
    @Autowired
    private ApplicationEventPublisher applicationEventPublisher;
    /**
     * 查询需要检查的伙伴的稿件列表
     * @param model
     * @param pageNo
     * @param pageSize
     * @param draft
     * @return
     */
    @RequestMapping
    public String index(Model model,String createTimeSpace,
                        @RequestParam(value = "pageNo", required = false, defaultValue = "1") Integer pageNo,
                        @RequestParam(value = "pageSize", required = false, defaultValue = "20") Integer pageSize,
                        Draft draft) {
        IPage<Draft> pageInfo = draftService.partnerIndex(pageNo,pageSize,draft,createTimeSpace);
        setDraftType(model);
        setDraftCheckStatus(model);
        model.addAttribute("roleId",draftService.selectRoleId());//roleId
        model.addAttribute("stateDic", StateDic.stateDic());
        model.addAttribute("time",createTimeSpace);
        model.addAttribute("searchInfo", draft);//查询条件前端回显
        model.addAttribute("pageInfo", new PageInfo(pageInfo));//分页数据
        return prefix+"partner-draft";
    }

    /**
     * 通过审核
     * @param model
     * @param  draftPre
     * @return
     */
    @PostMapping("check")
    @ResponseBody
    public AjaxResult check(Model model,Draft draftPre){
        log.info("draftPre实体-->{}",draftPre);
        Long draftId = draftPre.getId();
        return toAjax(draftService.passUtil(draftId,draftPre));
    }

    /**
     * 修改稿件内容
     */
    @PostMapping("edit")
    @ResponseBody
    public AjaxResult edit(Model model, String draftId,Draft draft){
        Draft draft1 = draftService.getById(draft.getId());
        draft1.setDraftNum(draft.getDraftNum());
        draft1.setDraftTitle(draft.getDraftTitle());
        if(draft.getDraftText()!=null) {
            draft1.setDraftText(draft.getDraftText());
        }
        draft1.setFileName(draft.getFileName());
        //操作日志
        applicationEventPublisher.publishEvent(new DraftLog().setActionType(DraftLogConstant.UPDATE)
                .setDraftId(draft.getId()));
        return toAjax(draftService.updateById(draft1));
    }

    /**
     * 稿件类型数据回显
     * @param model
     */
    public void setDraftType(Model model){
        Map<Integer,String> types = new HashMap<Integer,String>();
        types.put(DraftType.Image_Text,"图文");
        types.put(DraftType.QA,"问答");
        types.put(DraftType.PPT,"PPT");
        types.put(DraftType.Audio,"音频");
        types.put(DraftType.VIDEO,"视频稿件");
        types.put(DraftType.WORD,"文字稿件");
        types.put(DraftType.SHORT,"短篇微博问答");
        types.put(DraftType.MID,"中篇微博问答");
        types.put(DraftType.LENGHT,"长篇微博问答");
        types.put(DraftType.ACCOUNTS,"公众号文章");
//        types.put(DraftType.SVIDEO,"视频稿件");
        model.addAttribute("draftTypes",types);
    }

    /**
     * 稿件审核状态回显
     * @param model
     */
    public void setDraftCheckStatus(Model model){
        Map<Integer,String> status = new HashMap<Integer,String>();
        status.put(DraftIsPass.Pass,"已通过");
        status.put(DraftIsPass.NotPass,"未通过");
        status.put(DraftIsPass.NotCheck,"未审核");
        model.addAttribute("draftCheckStatus",status);
    }

    /**
     * 查询需要检查的伙伴的稿件列表
     * @param model
     * @param pageNo
     * @param pageSize
     * @param draft
     * @return
     */
    @RequestMapping("video")
    public String video(Model model,String createTimeSpace,
                        @RequestParam(value = "pageNo", required = false, defaultValue = "1") Integer pageNo,
                        @RequestParam(value = "pageSize", required = false, defaultValue = "20") Integer pageSize,
                        Draft draft) {
        IPage<Draft> pageInfo = draftService.partnerVideoIndex(pageNo,pageSize,draft,createTimeSpace);
        setDraftType(model);
        setDraftCheckStatus(model);
        model.addAttribute("roleId",draftService.selectRoleId());//roleId
        model.addAttribute("stateDic", StateDic.stateDic());
        model.addAttribute("time",createTimeSpace);
        model.addAttribute("searchInfo", draft);//查询条件前端回显
        model.addAttribute("pageInfo", new PageInfo(pageInfo));//分页数据
        return prefix+"partner-video";
    }


}
